package com.sanji.sjmallmodule.phoneshell;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallmodule.order.AliPayWebViewActivity;
import com.sanji.sjmallutils.Tools;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 手机壳支付
 * Created by Administrator on 2017/9/16.
 */

public class ShellPayActivity extends BaseActivity implements View.OnClickListener,NoHttpListener{

    @BindView(R.id.common_title_tv)TextView commonTitleTv;
    @BindView(R.id.common_back_img)ImageView commonBackImg;
    @BindView(R.id.shell_pop_payment_coast)TextView amountTv;
    @BindView(R.id.shell_pop_payment_submit)Button submitBtn;

    private String orderIds;
    private String payNumber;
    private double amount;
    private String uid;
    private String payUrl;
//    private int[] ids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shell_pay);
        ButterKnife.bind(this);

        Intent data = getIntent();
        orderIds = data.getStringExtra("ids");
        payNumber = data.getStringExtra("payNum");
        amount = data.getDoubleExtra("amount",0);
        uid = myShare.getString(ConfigConts.USER_ID,"");
        initViews();
    }

    private void initViews(){
        commonTitleTv.setText("订单支付");
        commonBackImg.setOnClickListener(this);
        amountTv.setText(getString(R.string.price_title) + Tools.divide(amount));
        submitBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.common_back_img:
                finishActivity();
                break;
            case R.id.shell_pop_payment_submit:

                payUrl = ConfigAPI.PHONE_SHELL_ORDER_PAY + "?ids="+orderIds+"&payNumber="+payNumber;
                Request<JSONObject> payRequest = NoHttp.createJsonObjectRequest(payUrl, RequestMethod.POST);
                payRequest.addHeader("X-Authorization-MemberId",uid);
                payRequest.setCancelSign(payUrl.hashCode());
                NoHttpCallBack.getInstance().add(this, payUrl.hashCode(), payRequest, this, true, true);
                break;
        }
    }

    @Override
    public void onSuccessed(int what, Response response) {
        if (what == payUrl.hashCode()){
            JSONObject jsonObject = (JSONObject) response.get();
            String status = jsonObject.optString("status");
            if (ConfigConts.STATUS_SUCCESS.equals(status)){
                String alipayUrl = jsonObject.optString("result");
                intent = new Intent(this, AliPayWebViewActivity.class);
                intent.putExtra("aliUrl", alipayUrl);
                startActivityForResult(intent, ConfigConts.ACTION_ALIPAY_RESULT);
            }else{
                String error = jsonObject.optString("errorMsg");
                Tools.toastTools(mCtx, error);
            }
        }
    }

    @Override
    public void onFailed(int what, Response response) {
        Tools.toastTools(mCtx, getString(R.string.request_false)+ response.get().toString());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ConfigConts.ACTION_ALIPAY_RESULT && resultCode == RESULT_OK) {
            //启动支付

            AlertDialog.Builder dialog = new AlertDialog.Builder(mCtx);
            dialog.setTitle("支付返回");
            dialog.setMessage("支付完成后，请根据您的支付情况点击下面的按钮");
            dialog.setPositiveButton("支付完成", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
//                    orderQuery();
                    Intent data = getIntent();
                    setResult(RESULT_OK,data);
                    dialog.dismiss();

                }
            });
            dialog.setNegativeButton("支付遇到问题", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog.create();
            dialog.show();

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
